package logic

import (
	"context"
	"dinling/dinlingrpc/dinlingrpc"
	"dinling/dinlingrpc/internal/svc"
	"dinling/model/mysql"
	"errors"
	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsAddLogic {
	return &GoodsAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsAddLogic) GoodsAdd(in *dinlingrpc.GoodsAddRequest) (*dinlingrpc.GoodsAddResponse, error) {
	// todo: add your logic here and delete this line
	typer := mysql.Typer{
		TyperName: in.TyperName,
	}
	err := typer.TyperFind(mysql.DB, in.TyperName)
	if err != nil {
		return nil, errors.New("类型添加失败")
	}
	goods := mysql.Goods{
		Title: in.Title,
		Name:  in.Name,
		Price: float64(in.Price),
		Typer: int64(typer.ID),
	}
	err = goods.GoodsAdd(mysql.DB)
	if err != nil {
		return nil, errors.New("添加失败")
	}
	return &dinlingrpc.GoodsAddResponse{
		GoodsId: int64(goods.ID),
	}, nil
}
